package com.nandi.emergencyinspection.ui.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.bean.TaskListBean
import com.nandi.emergencyinspection.UpdateListEvent
import com.nandi.emergencyinspection.mvp.TaskListContract
import com.nandi.emergencyinspection.mvp.TaskListPresenter
import com.nandi.emergencyinspection.ui.activity.TaskInfoActivity
import com.nandi.emergencyinspection.ui.adapter.ListAdapter

import com.tbright.ktbaselibrary.mvp.BaseMvpFragment
import kotlinx.android.synthetic.main.fragment_1.view.*
import org.greenrobot.eventbus.Subscribe

private const val ARG_PARAM1 = "param1"

class Fragment1 : BaseMvpFragment<TaskListPresenter>(), TaskListContract.TaskListView {
    private val titles: ArrayList<TaskListBean.RecordsBean?> = ArrayList()
    private var listAdapter: ListAdapter = ListAdapter(titles)
    var page = 1
    private var size = 10
    private var param1: Int? = null
    lateinit var mView: View
    override fun getLayoutId(): Int {
        return R.layout.fragment_1
    }

    override fun initData() {

    }

    override fun initView(savedInstanceState: Bundle?) {
        getList()
    }

    //    private var mView:View? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getInt(ARG_PARAM1) + 1
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = inflater.inflate(R.layout.fragment_1, container, false)
        initAdapter(mView)
        return mView
    }

    /**
     * 刷新
     */
    private fun initRefreshLayout(mView: View) {
        mView.swipe.setColorSchemeColors(Color.rgb(47, 223, 189))
        mView.swipe.setOnRefreshListener { // 这里的作用是防止下拉刷新的时候还可以上拉加载
            listAdapter.loadMoreModule.isEnableLoadMore = false
            // 下拉刷新，需要重置页数
            page = 1
            //请求数据
            getList()
        }
    }

    private fun getList() {
        mPresenter?.getList(page = page, size = size, status = param1)
    }

    /**
     * 初始化列表适配器
     */
    private fun initAdapter(mView: View) {
//        listAdapter.addChildClickViewIds(R.id.text_img, R.id.audio_img)
        listAdapter.setOnItemChildClickListener { adapter, view, position ->
            val any = adapter.data[position] as String
            when (view.id) {

            }
        }
        listAdapter.setOnItemClickListener { adapter, view, position ->
            val recordsBean = adapter.data[position] as TaskListBean.RecordsBean
            startActivity(
                Intent(activity, TaskInfoActivity::class.java).putExtra(
                    "data",
                    recordsBean
                )
            )
        }
        mView.dataShow.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = listAdapter
        }
        listAdapter.setEmptyView(R.layout.emputy_layout)
        initLoadMore(mView)
        initRefreshLayout(mView)
    }

    /**
     * 初始化上拉加载
     */
    private fun initLoadMore(mView: View) {
        listAdapter.loadMoreModule.setOnLoadMoreListener {
            mView.swipe.isRefreshing = false
            listAdapter.loadMoreModule.isEnableLoadMore = true
            getList()
        }
        listAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        listAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    /**
     * 设置数据到适配器
     */
    private fun setData(data: List<TaskListBean.RecordsBean>?) {
        //省略网络请求数据......
        //网络请求成功后下拉刷新与上拉加载都做如下处理:

        mView.swipe.isRefreshing = false
        listAdapter.loadMoreModule.isEnableLoadMore = true
        if (1 === page) {
            //如果是加载的第一页数据，用 setData()
            listAdapter.setList(data)
        } else {
            //不是第一页，则用add
            if (data != null) {
                listAdapter.addData(data)
            }

        }
        var dataSize = 0
        if (data != null) {
            dataSize = data.size
        }
        if (dataSize < size) {
            //如果不够一页,显示没有更多数据布局
            listAdapter.loadMoreModule.loadMoreEnd()
        } else {
            listAdapter.loadMoreModule.loadMoreComplete()
        }
        // page加一
        ++page
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int) =
            Fragment1().apply {
                arguments = Bundle().apply {
                    putInt(ARG_PARAM1, param1)

                }
            }
    }
    //处理粘性消息

    @Subscribe(threadMode = org.greenrobot.eventbus.ThreadMode.MAIN)
    fun onUpdateEvent(messageEvent: UpdateListEvent<*>) {
        // 下拉刷新，需要重置页数
        page = 1
        //请求数据
        getList()
    }

    override fun showResult(result: TaskListBean?) {
        setData(result?.records)
    }
}